import Vue from 'vue'
import VueRouter from 'vue-router'
import Docs from '../views/list-page/doc/index.vue'
import Files from '../views/list-page/file/index.vue'
import Imgs from '../views/list-page/img/index.vue'
import Musics from '../views/list-page/music/index.vue'
import Recycles from '../views/list-page/recycle/index.vue'
import Shares from '../views/list-page/share/index.vue'
import Videos from '../views/list-page/video/index.vue'

import Other from '../views/list-page/other/index.vue'
import Layout from '../layout/index.vue'
import Login from '../views/login/index.vue'
import Register from '../views/register/index.vue'
import Forget from '../views/forget/index.vue'
import Share from '../views/share/index.vue'
import PreviewCode from '../views/preview/code/index.vue'
import PreviewOffice from '../views/preview/office/index.vue'
import PreviewIframe from '../views/preview/iframe/index.vue'
import PreviewMusic from '../views/preview/music/index.vue'
import PreviewVideo from '../views/preview/video/index.vue'
import Error404 from '../views/error-page/404/index.vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '../utils/cookie'
import store from '../store/index'
import userService from '../api/user/index'
import { Message } from 'element-ui'

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: Layout,
    children: [
      {
        path: '/docs',
        name: 'Docs',
        component: Docs,
      },
      {
        path: '/files',
        name: 'Files',
        component: Files,
      },
      {
        path: '/imgs',
        name: 'Imgs',
        component: Imgs,
      },
      {
        path: '/musics',
        name: 'Musics',
        component: Musics,
      },
      {
        path: '/recycles',
        name: 'Recycles',
        component: Recycles,
      },
      {
        path: '/shares',
        name: 'Shares',
        component: Shares,
      },
      {
        path: '/videos',
        name: 'Videos',
        component: Videos,
      },
      {
        path: '/other',
        name: 'Other',
        component: Other,
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
  },
  {
    path: '/forget',
    name: 'Forget',
    component: Forget,
  },
  {
    path: '/share/:shareId',
    name: 'Share',
    component: Share,
  },
  {
    path: '/preview/code/:fileId',
    name: 'PreviewCode',
    component: PreviewCode,
  },
  {
    path: '/preview/office/:fileId',
    name: 'PreviewOffice',
    component: PreviewOffice,
  },
  {
    path: '/preview/iframe/:fileId',
    name: 'PreviewIframe',
    component: PreviewIframe,
  },
  {
    path: '/preview/music/:parentId/:fileId',
    name: 'PreviewMusic',
    component: PreviewMusic,
  },
  {
    path: '/preview/video/:parentId/:fileId',
    name: 'PreviewVideo',
    component: PreviewVideo,
  },
  {
    path: '/404',
    component: Error404,
  },
  { path: '*', redirect: '/404', hidden: true },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

const toIndexPageList = ['Login', 'Register', 'Forget'],
  whiteList = ['Login', 'Register', 'Forget', 'Share']

router.beforeEach((to, from, next) => {
  NProgress.start()
  const hasToken = getToken()
  if (hasToken) {
    if (toIndexPageList.indexOf(to.name) !== -1) {
      next({ name: 'Index' })
    } else {
      let redirect = from.query.redirect
      if (!redirect || to.path === redirect) {
        // 防止有token直接跳转首页的情况下没有初始化用户信息的情况
        if (!store.getters.username) {
          userService.info(
            (res) => {
              store.commit('file/setFileParentId', res.data.rootFileId)
              store.commit('file/setFileDefaultParentId', res.data.rootFileId)
              store.commit(
                'file/setDefaultParentFilename',
                res.data.rootFilename
              )
              store.commit('user/setUsername', res.data.username)
              next()
            },
            (res) => {
              Message.error(res.message)
            }
          )
        } else {
          next()
        }
      } else {
        next({
          path: redirect,
        })
      }
    }
  } else {
    if (whiteList.indexOf(to.name) !== -1) {
      next()
    } else {
      next({
        name: 'Login',
        query: {
          redirect: to.fullPath,
        },
      })
    }
  }
  NProgress.done()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
